package com.dgcsxy.my_college_note.controller;


import com.dgcsxy.my_college_note.common.utils.ResponseResult;
import com.dgcsxy.my_college_note.entity.dto.log.LogInfoDto;
import com.dgcsxy.my_college_note.entity.pojo.McnLog;
import com.dgcsxy.my_college_note.entity.vo.log.LogAddVo;
import com.dgcsxy.my_college_note.entity.vo.log.LogUpdateVo;
import com.dgcsxy.my_college_note.service.McnLogService;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Yjx
 * @since 2022-10-24
 */
@RestController
@RequestMapping("/api/log")
@CrossOrigin
public class McnLogController {

    private final McnLogService mcnLogService;

    public McnLogController(McnLogService mcnLogService) {
        this.mcnLogService = mcnLogService;
    }

    @PostMapping("/add")
    public ResponseResult addLog(@RequestBody LogAddVo logAddVo) {
        String message = mcnLogService.addLog(logAddVo);
        return ResponseResult.SUCCESS(message);
    }

    @PutMapping("/delete/{logId}")
    public ResponseResult deleteLog(@PathVariable("logId") String logId) {
        String message = mcnLogService.deleteLog(logId);
        return ResponseResult.SUCCESS(message);
    }

    @PutMapping("/update")
    public ResponseResult updateLog(@RequestBody LogUpdateVo logUpdateVo) {
        String message = mcnLogService.updateLog(logUpdateVo);
        return ResponseResult.SUCCESS(message);
    }

    @GetMapping("/get/{logId}")
    public ResponseResult selectLog(@PathVariable("logId") String logId) {
        LogInfoDto logInfoDto = mcnLogService.selectLog(logId);
        return logInfoDto!=null?ResponseResult.SUCCESS(logInfoDto)
                :ResponseResult.FAILED("查无次日记");
    }

    @GetMapping("/getAllLog/{userId}")
    public ResponseResult selectAllLog(@PathVariable("userId") String userId) {
        List<McnLog> mcnLogs = mcnLogService.selectAllLog(userId);
        return mcnLogs!=null?ResponseResult.SUCCESS(mcnLogs)
                :ResponseResult.FAILED("请先登录再查看日记");
    }

}

